<?php

require_once 'newsfeed.class.php';

/**
 * php-newsfeed manager class
 * provides a newsfeed html tag using jquery or the marquee tag 
 * @since 2011
 * @author Jerome Lamartiniere
 * 
 * This file is part of php-newsfeed.
 *
 * php-newsfeed is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License.
 * php-newsfeed is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with php-newsfeed.  If not, see <http://www.gnu.org/licenses/>
 */

class newsfeedManager {
	
	/**
	 * Input newsfeed object
	 */
	private $_oNewsFeed;
	
	/**
	 * Form action
	 */
	private $_sFormAction = '#';
	
	/**
	 * Div id
	 */
	private $_sDivId = 'newfeedForm';
	
	/**
	 * Constructor
	 */
	public function __construct($aParams) {
		
		if (! isset ( $aParams ['oNewsFeed'] ) || ! is_a ( $aParams ['oNewsFeed'], 'newsfeed' )) {
			throw new Exception ( 'missing or bad input newsfeed', 10 );
		} else {
			$this->_oNewsFeed = $aParams ['oNewsFeed'];
		}
		
		if (isset ( $aParams ['sFormAction'] )) {
			if (! is_string ( $aParams ['sFormAction'] )) {
				throw new Exception ( 'bad form action', 11 );
			} else {
				$this->_sFormAction = $aParams ['sFormAction'];
			}
		}
		
		if (isset ( $aParams ['sDivId'] )) {
			if (! is_string ( $aParams ['sDivId'] )) {
				throw new Exception ( 'bad form id', 11 );
			} else {
				$this->_sDivId = $aParams ['sDivId'];
			}
		}
	}
	
	/**
	 * Returns an xhtml form from newsfeed object items
	 */
	public function getForm($bRender = false) {
		
		$sForm = "\n" . '<div id="' . $this->_sDivId . '"><form method="post" action="' . $this->_sFormAction . '">';
		foreach ( $this->_oNewsFeed->getItems () as $iKey => $aItem ) {
			$sForm .= "\n" . '<fieldset>' . "\n" . '<legend>Item&nbsp;' . $iKey . '</legend>';
			foreach ( $aItem as $sType => $aAttr ) {
				$sForm .= "\n" . '<fieldset>' . "\n" . '<legend>Tag &lt;' . $sType . '&gt;</legend>';
				foreach ( $aAttr as $sName => $sValue ) {
					$sForm .= "\n" . '<label>' . $sName . ': ' . '</label><input type="text" name="aItems[' . $iKey . '][' . $sType . '][' . $sName . ']" value="' . $sValue . '" />';
				}
				$sForm .= "\n" . '</fieldset>';
			}
			$sForm .= "\n" . '</fieldset>';
		}
		$sForm .= "\n" . '<p><input type="submit"/></p>' . "\n" . '</form>' . "\n" . '</div>';
		
		if ($bRender) {
			echo $sForm;
		}
		
		return $sForm;
	
	}
	
	public function getNewsfeed() {
		
		return $this->_oNewsFeed;
	
	}

}

?>